package com.feign.feignClient.controller;

import com.alibaba.fastjson.JSONObject;
import com.feign.feignClient.entity.Person;
import com.feign.feignClient.entity.Result;
import com.feign.feignClient.entity.UserInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
public class HelloController {


    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }


    @PutMapping("/person/{id}")
    public Person person(@PathVariable Long id) {
        Random r = new Random();
        Person p = new Person();
        p.setId(id);
        p.setName(r.nextInt(1000)+"");
        return p;
    }

    @PostMapping("/personSaveJson")
    public Map personSaveJson(@RequestBody Person person) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("data", JSONObject.toJSONString(person));
        return map;
    }

    @PostMapping(value = "/userSaveXml",
            consumes = MediaType.APPLICATION_XML_VALUE,
            produces = MediaType.APPLICATION_XML_VALUE)
    public Result hello(@RequestBody UserInfo user) {
        System.out.println("-----------------------------"+user );
        Result r = new Result();
        r.setCode(0);
        return r;
    }

    @GetMapping(value = "/test",
            produces = MediaType.APPLICATION_XML_VALUE)
    public Result test( ) {
        Result r = new Result();
        r.setCode(0);
        return r;
    }

}
